package com.kd.controller.jobSeeker;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.kd.bean.jobSeeker.Basic_information;
import com.kd.bean.jobSeeker.Deliver_resume;
import com.kd.bean.recruiter.Recruiter_basic;
import com.kd.bean.recruiter.Recruitment_msg;
import com.kd.service.jobSeeker.IBasic_informationService;
import com.kd.service.jobSeeker.IDeliver_resumeService;
import com.kd.service.recruiter.IRecruitment_msgService;

/**
 * 求职者相关的类
 * --投递简历表（Deliver_resumeController）--
 * @author ZFC
 *
 */
@Controller
@RequestMapping("/deliver_resume")
public class Deliver_resumeController {

	@Autowired
	private IDeliver_resumeService delService;
	
	@Autowired
	private IRecruitment_msgService recMsgService;//招聘者相关的类 - 招聘信息（Recruiter_basic）--
	
	@RequestMapping("/queryAllDeliverMessageByRecruiter_id")
	@ResponseBody
	public PageInfo<Deliver_resume> queryAllDeliverMessageByRecruiter_id(Integer pageNum,Integer pageSize,HttpSession session){
		//招聘者查询简历 通过招聘者的id

		Recruiter_basic basic = (Recruiter_basic) session.getAttribute("basicMsg");
		PageInfo<Deliver_resume> info = null;
		try {
			info = delService.queryAllDeliverMessageByRecruiter_id(pageNum, pageSize, basic.getRecruiter_id());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return info;
	}
	@RequestMapping("/updateDeliveDr_state")
	@ResponseBody
	public Boolean updateDeliveDr_state(Integer dr_id,Integer dr_state) {
		//修改求职者的面试申请状态 1、简历申请未通过 2、简历申请通过<发送面试邀请>

		Boolean flag = false;
		try {
			flag = delService.updateDeliveDr_state(dr_id, dr_state);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
	@RequestMapping("/updateDr_delete_state")
	@ResponseBody
	public Boolean updateDr_delete_state(Integer dr_id,Integer dr_delete_state) {
		//删除投递简历删除状态 1、未删除 2、已删除

		Boolean flag = false;
		try {
			flag = delService.updateDr_delete_state(dr_id, dr_delete_state);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
	@RequestMapping("/downLoad")
	public ResponseEntity<byte[]> downLoadFile(@RequestParam("fileName") String fileName,
			HttpServletRequest request)
			throws Exception {
		// 获得下载文件的文件夹
		String stroeHome = System.getProperty("user.dir") + "/data/tmp/fileHome";
		File file = new File(stroeHome + File.separator + fileName);
		String fileNameStr = new String(fileName.getBytes("UTF-8"), 
				"ISO-8859-1");
		// 设置头部
		HttpHeaders headers = new HttpHeaders();
		headers.setContentDispositionFormData("attachment", fileNameStr);
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
				headers, HttpStatus.CREATED);
	}
	
	@RequestMapping("/touDi")
	@ResponseBody
	public Map<String, Object> touDi(Integer[] ids,Integer rec_id,HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			//通过招聘信息id查询招聘信息
			Recruitment_msg recruitment_msg = recMsgService.queryByRec_id(rec_id);
			if(recruitment_msg.getRec_msg_check_state()>=2) {
				//审核状态w未通过
				map.put("msg", "审核状态未通过");
				return map;
			}
			if(recruitment_msg.getRec_msg_state()==2) {
				//该招聘信息以下架
				map.put("msg", "该招聘信息以下架");
				return map;
			}
			Recruiter_basic basic = (Recruiter_basic) session.getAttribute("basicMsg");
			
			//一键投递简历   (basic_id<应聘者id> basic.getRecruiter_id<招聘者id>  recruitment_msg<招聘信息>)
			Boolean flag = delService.insertDeliver_resume(ids, basic.getRecruiter_id(), recruitment_msg);
			if(flag) {
				map.put("msg", "发送成功");
				return map;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		map.put("msg", "发送失败，请稍后在试,可能存在有用户没有完善文档简历！！");
		return map;
	}
	
	@RequestMapping("/insertDeliver_resumeOne")
	@ResponseBody
	public Boolean insertDeliver_resumeOne(Deliver_resume deliver_resume,HttpSession session) {
		Basic_information basic_information = (Basic_information) session.getAttribute("basicMessage");
		Boolean flag = false;
		try {
			flag = delService.insertDeliver_resumeOne(deliver_resume,basic_information.getBasic_id());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
	@RequestMapping("/queryDeliver_resumeByDoc_id")
	@ResponseBody
	public PageInfo<Deliver_resume> queryDeliver_resumeByDoc_id(Integer pageNum,Integer pageSize,HttpSession session){
		Basic_information basic_information = (Basic_information) session.getAttribute("basicMessage");
		PageInfo<Deliver_resume> info = null;
		try {
			info = delService.queryDeliver_resumeByDoc_id(pageSize, pageNum, basic_information.getBasic_id());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return info;
	}
	
	@RequestMapping("/updateDr_download_state")
	@ResponseBody
	public Boolean updateDr_download_state(Integer dr_download_state,Integer dr_id) {
		Boolean flag = false;
		try {
			flag = delService.updateDr_download_state(dr_download_state,dr_id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
}
